// pages/user-appointment-record/index.js
const handler = require("../../utils/function.js");
const { $Message } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    spinShow:true,
    visible5: false,
    actions5: [
      {
        name: '取消'
      },
      {
        name: '支付',
        color: '#19be6b',
        loading: false
      }
    ],
    recordData:[],
    user_type: 0,
    current_page:1,
    total_page:0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取数据
    var id = wx.getStorageSync('user_info').id;
    var user_type = wx.getStorageSync('user_info').user_type;
    this.initData(id, user_type);
  },
  cancelOrder:function(e){
    var oid = e.currentTarget.dataset.oid;
    var that = this;
    wx.showModal({
      
      content: '是否取消订单',
      success:function(e){
        if(e.confirm){
          handler.requestData("cancelOrder", { oid: oid }, function (res) {
            if (res.success == true) {
              $Message({
                content: res.message,
                type: 'success'
              });
              that.onPullDownRefresh();
            } else {
              wx.showToast({
                title: res.message,
                icon: 'loading'
              })
            }
          });
        }
      },
      fail:function(){}
    })
    
  },
  initData: function (id, user_type) {
    var para = {
      user_id:id,
      user_type: user_type,
      current_page: this.data.current_page
    };
    var that = this;
    that.setData({
      user_type: user_type
    })
    handler.requestData("appointmentRecord", para, function (res) {
      
      if (res.success == true) {
        that.setData({
          spinShow: false,
          recordData:res.data.data,
          total_page:res.data.total
        })
        console.log(that.data.recordData)
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });
   
  },
  //取消已付款的订单
  cancelPaymentOrder:function(e){
    var oid = e.currentTarget.dataset.oid;
    var that = this;
    wx.showModal({
      content: '取消订单',
      success:function(e){
        if (e.confirm){
          handler.requestData("cancelPaymentOrder", { oid: oid }, function (res) {
            if (res.success == true) {
              $Message({
                content: res.message,
                type: 'success'
              });
              that.onPullDownRefresh();
            } else {
              wx.showToast({
                title: res.message,
                icon: 'loading'
              })
            }
          });
        }
        
      },
      fail:function(){}
    })
    

  },
  // 获取数据
  get_data() {
    var that = this;
    var i = 0;
    var time = setInterval(() => {
      if (i < 5) { i++; console.log() }
      else { clearInterval(time); that.setData({ spinShow: false }) }
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  // pay-button 支付按钮
  pay_button: function (e) {
    var oid = e.currentTarget.dataset.oid;
    var submitJson ={
      'openid':wx.getStorageSync('user_info').openid,
      'oid':oid
    };   
  var that = this;
    handler.requestData("wfkOrder", submitJson, function (res) {
    if (res.success == true) {
      $Message({
        content: res.message,
        type: 'success'
      });
      that.setData({
        payPara: res.data
      })
      that.handleOpen5()
    } else {
      $Message({
        content: res.message,
        type: 'error'
      });
    }
  }); 
  },

  handleOpen5() {
    this.setData({
      visible5: true
    });
  },

  handleClick5({ detail }) {
    if (detail.index === 0) {
      this.setData({
        visible5: false
      });
    } else {
      const action = [...this.data.actions5];
      // action[1].loading = true;

      this.setData({
        actions5: action
      });
      var that =this;
      wx.requestPayment({
        'timeStamp': '' + this.data.payPara.timeStamp + '',
        'nonceStr': '' + this.data.payPara.nonceStr + '',
        'package': '' + this.data.payPara.package + '',
        'signType': 'MD5',
        'paySign': '' + this.data.payPara.paySign + '',
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: '支付成功',
            icon: 'loading'
          })
          that.setData({ visible5:false})
          that.onPullDownRefresh();
          // handler.intoPageHandler("../user-appointment-record/index");
        },
        fail: function (res) {
          that.setData({ visible5: false })
          wx.showToast({
            title: '支付失败',
            icon: 'loading'
          })

        }
      })
      // setTimeout(() => {
      //   action[1].loading = false;
      //   this.setData({
      //     visible5: false,
      //     actions5: action
      //   });
      //   $Message({
      //     content: '支付成功！',
      //     type: 'success'
      //   });
      // }, 2000);
    }
  },
  handler_next_page(e) {
    var id = e.currentTarget.dataset.uid;
    handler.intoPageHandler(e.currentTarget.dataset.url);
  },
  call_phone_handler:function(e){
    handler.callPhoneHandler(e.currentTarget.dataset.phone);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    var id = wx.getStorageSync('user_info').id;
    var user_type = wx.getStorageSync('user_info').user_type;
    this.initData(id, user_type);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉加载更多
    var current_page = this.data.current_page;
    var total_page = this.data.total_page;
    if (total_page < current_page * 10){
      console.log("已经是全部数据");
    }else{
      this.setData({
        current_page: current_page+1
      })
      wx.showNavigationBarLoading() //在标题栏中显示加载
      var id = wx.getStorageSync('user_info').id;
      var user_type = wx.getStorageSync('user_info').user_type;
      this.initData(id, user_type);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})